
<#include "/head.ftl" />
<div id="pg_margins">
<div id="col1"><#include "/sidebar.ftl" /></div>
<div id="col2">

<div class="location">用户管理</div>
  
<div class="massaction"><a href="javascript:addUser();">添加</a></div>

<div style="float:left;width:100%;">
	<table id="listTable" class="table table-striped table-bordered table-hover bd_c_1" cellpadding="0" cellspacing="0"></table>
</div>

</div>			
</div>
<#include "modal.ftl" />
<script>
    var $table = $('#listTable');
    var $param = {};

    $(function () {
        // bootstrap table初始化
        $param = {
            url: '/user/list',
            striped: false,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            smartDisplay: false,
            striped: true,
            idField: 'uid',
            sortName: 'uid',
            sortOrder: 'desc',
            onLoadSuccess: function() {
            	$('#listTable thead tr').attr("class", "headings");
            },
            columns: [
                {field: 'uid', title: '编号', align: 'center', valign: 'middle'},
                {field: 'name', title: '真实姓名', align: 'center', valign: 'middle'},
                {field: 'rname', title: '所属分组', align: 'center', valign: 'middle'},
                {field: 'email', title: '电子邮箱', align: 'center', valign: 'middle'},
                {field: 'telphone', title: '联系电话', align: 'center', valign: 'middle'},
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    formatter: 'statusFormatter'
                },
                {
                    field: 'access',
                    title: '最后登陆时间',
                    align: 'center',
                    valign: 'middle',
                    formatter: 'timeFormatter'
                },
                {field: 'ip', title: '最后登陆ip', align: 'center', valign: 'middle'},
                {
                    field: 'uid',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: 'operateFormatter',
                    clickToSelect: false
                }
            ]
        };
        $table.bootstrapTable($param);
    });

    function statusFormatter(value, row, index) {
        var status = "";
        if (value == '1') {
            status = '<span class="green">启用</span>';
        } else if (value == '0') {
            status = '<span class="red">禁用</span>';
        } else {
            status = '-';
        }
        return status;
    }

    function operateFormatter(value, row, index) {
        var action = [];
        action.push('<a href="javascript:;" onclick="updateUser(' + row.uid + ')">修改</a>');
        action.push('<a href="javascript:;" onclick="deleteUser(' + row.uid + ')">删除</a>');
        return action.join('&nbsp;&nbsp;|&nbsp;&nbsp;');
    }

    function searchUser() {
        $table.bootstrapTable('destroy').bootstrapTable($param);
    }
    
    function getRoles() {
    	if ($("#saveUser #roles").html()) {
            $("#saveUser [name='rid']").val($("#saveUser #rid").val());
    		return;
    	}
        $.get("/role/list?limit=100", function(res) {
            if (!res.rows) {
                return false;
            }
            var options = '<select name="rid" class="form-control">';
            options += '<option value="0">选择</option>';
            res.rows.forEach((row) => {
            	options += '<option value="' + row.rid + '">' + row.name + '</option>';
            });
            options += '</select>';
            $("#saveUser #roles").html(options);
            $("#saveUser [name='rid']").val($("#saveUser #rid").val());
        });
    }
    
    function getTypes() {
    	if ($("#saveUser #types").html()) {
            $("#saveUser [name='ptid']").val($("#saveUser #ptid").val());
    		return;
    	}
        $.get("/type/list?limit=100", function(res) {
            if (!res.rows) {
                return false;
            }
            var options = '<select name="ptid" class="form-control">';
            options += '<option value="0">选择</option>';
            res.rows.forEach((row) => {
            	options += '<option value="' + row.tid + '">' + row.name + '</option>';
            });
            options += '</select>';
            $("#saveUser #types").html(options);
            $("#saveUser [name='ptid']").val($("#saveUser #ptid").val());
        });
    }

    function addUser() {
        $("#saveUser #saveForm")[0].reset();
        $("#saveUser #saveForm #uid").val("");
        $("#saveUser #saveForm #rid").val("0");
        $("#saveUser #saveForm #ptid").val("0");
        $("#saveUser .modal-title").html("用户添加");
        $("#saveUser").modal("show");
        $(".modal").css("z-index", 998);
        $(".modal-backdrop").css("z-index", 997);
        getRoles();
        getTypes();
    }

    function updateUser(uid) {
        $("#saveUser #saveForm")[0].reset();
        $("#saveUser .modal-title").html("用户修改");
        $.get("/user/info?uid=" + uid, function(res) {
            if (!res.data) {
                showMsgFail(res.message ? res.message : "请求失败");
                return false;
            }
            $("#saveUser #uid").val(res.data.uid);
            $("#saveUser #name").val(res.data.name);
            $("#saveUser #email").val(res.data.email);
            $("#saveUser #telphone").val(res.data.telphone);
            $("#saveUser #status").val(res.data.status);
            $("#saveUser #rid").val(res.data.rid);
            $("#saveUser #rname").val(res.data.rname);
            $("#saveUser #ptid").val(res.data.ptid);
            $("#saveUser #pname").val(res.data.pname);
            $("#saveUser").modal("show");
            $(".modal").css("z-index", 998);
            $(".modal-backdrop").css("z-index", 997);
	        getRoles();
	        getTypes();
        });
    }
    
    function saveUser() {
    	var rname = $('#saveForm #rid').find("option:selected").text();
    	$('#saveForm #rname').val(rname == "选择" ? " " : rname);
    	var pname = $('#saveForm #ptid').find("option:selected").text();
    	$('#saveForm #pname').val(pname == "选择" ? " " : pname);
        var fields = $('#saveForm').serializeArray();
        var user = {};
        $.each(fields, function(index, field) {
            if (field.value) user[field.name] = field.value;
        })
        if (!user["name"] || !user["pass"]) {
            showMsgFail("用户名称、密码必填");
            return false;
        }
        if (user["pass"] && user["pass"] != user["repass"]) {
            showMsgFail("密码不一致");
            return false;
        }
        $.ajax({
            url: "/user/save",
            type: "POST",
            data: JSON.stringify(user),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(res) {
                if (res.code != "0200") {
                    showMsgFail(res.message ? res.message : "请求失败");
                    return false;
                }
                $("#saveUser").modal("hide");
                searchUser();
            },
            error: function(e) {
                showMsgFail("请求失败");
            }
        });
    }
    
    function deleteUser(uid) {
        confirm("确认删除该用户？", function() {
            $.ajax({
                url: "/user/delete?uid=" + uid,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(res) {
                    if (res.code != "0200") {
                        showMsgFail(res.message ? res.message : "请求失败");
                        return false;
                    }
                    searchUser();
                },
                error: function(e) {
                    showMsgFail("请求失败");
                }
            });
        });
    }
</script>
<#include "/foot.ftl" />
